﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Resto.CostControl.Core.Models.SysSettings;
using System.Data.Entity;
using System.Data.SqlClient;

namespace Resto.CostControl.Core.Services
{
    public class PrivilegeService
    {
        private DbContext context = null;
        public PrivilegeService(DbContext ctx)
        {
            context = ctx;
        }

        public IList<Privilege> GetUserPermissions(string userCode, string parentCode)
        {
            string sql = @"SELECT A.* FROM Privileges A 
                    INNER JOIN RolePrivilegeRelations C on A.PrivilegeCode=C.PrivilegeCode
                    INNER JOIN RoleUserRelations D ON C.RoleCode = D.RoleCode
                    WHERE D.UserCode=@UserCode and IsNull(A.ParentCode,'')=@ParentCode 
                    Order By a.SeqNo";

            List<SqlParameter> plist = new List<SqlParameter>();
            plist.Add(new SqlParameter("@UserCode", userCode));
            plist.Add(new SqlParameter("@ParentCode", parentCode ?? ""));

            var list = context.Database.SqlQuery<Privilege>(sql, plist.ToArray()).ToList();

            return list;
        }
    }
}
